package com.guat.service;

import com.guat.dao.ArticleDao;
import com.guat.dao.PictureDao;
import com.guat.dao.UserDao;
import com.guat.entity.Article;
import com.guat.entity.Picture;
import com.guat.entity.User;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PictureService {
    //上传图片
    public Map<String, Object> addPicture(String url,String info) {
        PictureDao pictureDao = new PictureDao();
        boolean flag = pictureDao.addPicture(url, info);
        Map<String, Object> map = new HashMap<>();
        if (flag){
            map.put("code",200);
            map.put("msg","图片上传成功！");
        }else {
            map.put("code",400);
            map.put("msg","图片上传失败");
        }
        return map;
    }

    //获取所有图片
    public List<Picture> getAll(){
        PictureDao pictureDao = new PictureDao();
        return pictureDao.getPictureAll();
    }

    //删除图片,注意删除数据库文件的同时，也要删除磁盘里的图片
    public Map<String, Object> deletePicture(String id){
        HashMap<String, Object> map = new HashMap<>();
        //先根据id查询出图片的信息,获取图片地址,删除图片
        PictureDao pictureDao = new PictureDao();
        //调用根据id查询一个图片信息方法
        Picture picture = pictureDao.getPictureById(id);
        //获取需要删除图片的文件名,地址=D:\Project\2023.9.5\my_demo\src\main\webapp\img\+文件名
        String path ="D:\\Project\\2023.9.5\\my_demo\\src\\main\\webapp\\img\\" + picture.getPictureUrl();
        //删除文件
        File file = new File(path);
        //检查文件是否存在
        if (file.exists()){
            //使用delete方法删除文件
            boolean result = file.delete();
            //判断文件是否删除成功
            if (result){
                //删除成功则调用pictureDao删除方法删除数据库中的数据
                boolean flag = pictureDao.deletePicture(id);
                if (flag) {
                    map.put("code",200);
                    map.put("msg","图片删除成功！");
                }else {
                    map.put("code",400);
                    map.put("msg","删除失败！图片不存在");
                }
            }
        }else {
            map.put("code",304);
            map.put("msg","图片文件未找到");
        }

        return map;
    }

    //根据id查询图片信息
    public Map<String, Object> doUpdatePicture(String id){
        PictureDao pictureDao = new PictureDao();
        Map<String, Object> map = new HashMap<>();
        Picture picture = pictureDao.doUpdatePicture(id);
        map.put("code",200);
        map.put("msg","查询成功！");
        map.put("data",picture);
        return map;
    }

    //根据id修改图片信息
    public Map<String, Object> updatePicture(String id,String picture_info){
        PictureDao pictureDao = new PictureDao();
        Map<String, Object> map = new HashMap<>();
        pictureDao.updatePicture(id, picture_info);
        map.put("code",200);
        map.put("msg","修改成功！");
        return map;
    }

    //根据搜索值获取文章
    public List<Picture> searchPicture(String value){
        PictureDao pictureDao = new PictureDao();
        return pictureDao.searchPicture(value);
    }
}
